/**
 * class name: ifind,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://jsease.googlecode.com/svn/trunk/core.js, ipop.js,
 * update: http://jsease.googlecode.com/svn/trunk/ifind.js
 */
function ifind(){/* {id, } */
    var A = arguments,
        _ = this;
    _ = ext( _, ext({
        id : 'ifind'+ now(3),
        elem: me.body,
        width:190,/* text input */
        name:'ifind',/* form item name */
        target:'',
        readonly:0,
        fontcase:''
        }, A[0] ) );

    _.uitype = 'ifind';

    _.elem = $(_.elem) || me.body;
    _.showed = false;
}
ifind.prototype = {
    hide : function(seled){
        var _ = this;
        if ( 1==seled ) {
            _.showed = _.focused = false;
            _.ipop.hide();
            1==_.bound && unbind(me, 'mousedown', function(){_.hide();});
            _.bound = 0;
        }
        if ( !_.showed && _.focused ) {
            return _;
        }
        if ( !_.showed && !_.focused ) {
            _.ipop.hide();
            1==_.bound && unbind(me, 'mousedown', function(){_.hide();});
            _.bound = 0;
        }

    return _;
    },

    show : function(msg){
        var _ = this;
        if ( 1==_.bound ) {
            return _;
        }
        _.poz = pos(_.$);

        _.ipop.show({css:'position:absolute;top:'+ _.poz._bottom +'px;left:'+ (_.poz.left+1) +'px;border:0;padding:0;width:'+ _.width +'px;-moz-border-radius:0;border-radius:0;z-index:6000;',data:_.body});

        if ( _.isel ) {
            _.iopt.scrollTop = _.isel.offsetTop;
        } else if ( _.val.value.length && _.ival.value.length ) {
            for ( var z=0,L=_.iopt.childNodes.length; z<L; z++ ) {
                var zo = _.iopt.childNodes[z];
                if ( attr(zo,'value')==_.val.value && attr(zo, 'text')==_.ival.value ) {
                    if ( _.isel ) {
                        _.isel.delClass('sel')
                    }
                    _.isel = zo.addClass('sel');
                    _.iopt.scrollTop = _.isel.offsetTop;
                }
            }
        }

        bind(me, 'mousedown', function(){_.hide();});

        _.bound = 1;
        _.showed = true;
    return _;
    },

    selval : function () {
        var _ = this;
        var value = hop(_, 'value') ? _.value : '',
            text = hop(_, 'text') ? _.text : '';
        _.val.value = value;
        _.ival.value = text;

        if ( _.target.nodeName == 'INPUT' ) {
            _.target.value = value;
        } else if ( _.target.nodeType ) {
            _.target.innerHTML = value;
        }

        if ( typeof _.onchange =='function' ) {
            _.onchange();
        }
    return _.hide(1);
    },

    init : function(){
        var _ = this;
        _.$ = elm('div').appendTo(_.elem).setClass('ifind');
        _.id = _.$.id = p(_, 'id') && !$(_.id) ? _.id : ('ifind_'+ now(3));
        _.$.style.width = _.width +'px';


        /* selector */
        _.cap = elm('div').setClass('cap').appendTo(_.$);
        _.tit = elm('div').setClass('tit').appendTo(_.cap);
        _.ival = elm('input').attr('type', 'text').attr('name', _.name).attr('value', '').setClass('ival').appendTo(_.tit);
        _.fontcase && _.ival.addClass(_.fontcase);
        _.readonly && _.ival.attr('readonly', 'readonly');

        _.sel = elm('div').setClass('sel').appendTo(_.cap);
        _.msa = elm('a').setClass('msa').attr('href', '#').appendTo(_.sel);


        _.body = elm('div').setClass('ifind').appendTo(me.body);
        _.body.style.width = (_.optwidth>_.width?_.optwidth:_.width) +'px';

        /* ipop.js required */
        _.ipop = (new ipop( { id: _.id +'_ipop', nomask: 1, notitle: 1, radius:0, autocenter:0, data: _.body } )).init();

        _.msa.onclick=function () {
            _.show();
            _.showed = false;
        return false;
        };

    return _;
    }

};